package com.ms.module.wechat.clear.fragment.details;

import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.navigation.fragment.NavHostFragment;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import com.ms.module.wechat.clear.R;
import com.ms.module.wechat.clear.base.BaseFragment;
import com.ms.module.wechat.clear.base.RxView;
import com.ms.module.wechat.clear.fragment.emoji.EmojiFragment;
import com.ms.module.wechat.clear.fragment.file.FileFragment;
import com.ms.module.wechat.clear.fragment.image.ImageFragment;
import com.ms.module.wechat.clear.fragment.video.VideoFragment;
import com.ms.module.wechat.clear.fragment.voice.VoiceFragment;
import com.ms.module.wechat.clear.repository.WeChatScanDataRepository;
import com.ms.module.wechat.clear.utils.MediaPlayerUtils;

import java.util.ArrayList;
import java.util.List;

public class DetailsFragment extends BaseFragment implements RxView.Action1<View> {

    private static final String TAG = "DetailsFragment";
    private ViewPager2 viewPager2;
    private ImageView imageViewBack;
    private TabLayout tabLayout;

    private String[] TITLES = new String[]{
            "视频",
            "图片",
            "语音",
            "文件",
            "表情"
    };


    private static DetailsFragment instance;


    public static DetailsFragment getInstance() {
        return instance;
    }

    public static DetailsFragment newInstance() {

        Bundle args = new Bundle();

        DetailsFragment fragment = new DetailsFragment();
        fragment.setArguments(args);
        instance = fragment;
        return fragment;
    }

    private List<Fragment> fragmentList = new ArrayList<>();


    @Override
    protected int getLayoutId() {
        return R.layout.fragment_details;
    }


    @Override
    protected void initView() {
        super.initView();
        viewPager2 = findViewById(R.id.viewPager2);
        imageViewBack = findViewById(R.id.imageViewBack);
        tabLayout = findViewById(R.id.tabLayout);
        // 取消指示器
        tabLayout.setSelectedTabIndicator(0);

        RxView.setOnClickListeners(this::onClick, imageViewBack);

    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        instance = this;

        fragmentList.clear();
        fragmentList.add(VideoFragment.newInstance());
        fragmentList.add(ImageFragment.newInstance());
        fragmentList.add(VoiceFragment.newInstance());
        fragmentList.add(FileFragment.newInstance());
        fragmentList.add(EmojiFragment.newInstance());


        for (int i = 0; i < TITLES.length; i++) {
            TabLayout.Tab tab = tabLayout.newTab();
            tabLayout.addTab(tab);
        }

        viewPager2.setOrientation(ViewPager2.ORIENTATION_HORIZONTAL);

        viewPager2.setAdapter(new FragmentStateAdapter(this) {
            @NonNull
            @Override
            public Fragment createFragment(int position) {
                Fragment fragment = fragmentList.get(position);
                return fragment;
            }

            @Override
            public int getItemCount() {
                return fragmentList.size();
            }
        });


        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {

                View view = tab.getCustomView();
                if (view == null) {
                    view = LayoutInflater.from(getActivity()).inflate(R.layout.tab_layout_header, null);
                    TextView textViewTitle = view.findViewById(R.id.textViewTitle);
                    TextView textViewCount = view.findViewById(R.id.textViewCount);
                    textViewTitle.setText(textViewTitle.getText());
                    textViewTitle.setTextColor(Color.parseColor("#000000"));
                    textViewCount.setTextColor(Color.parseColor("#000000"));
                    if (tab.getPosition() == 0) {
                        textViewCount.setText(WeChatScanDataRepository.getInstance().getVideoList().size() + "");
                    } else if (tab.getPosition() == 1) {
                        textViewCount.setText(WeChatScanDataRepository.getInstance().getImageList().size() + "");
                    } else if (tab.getPosition() == 2) {
                        textViewCount.setText(WeChatScanDataRepository.getInstance().getVoiceList().size() + "");
                    } else if (tab.getPosition() == 3) {
                        textViewCount.setText(WeChatScanDataRepository.getInstance().getFileList().size() + "");
                    } else if (tab.getPosition() == 4) {
                        textViewCount.setText(WeChatScanDataRepository.getInstance().getEmojiList().size() + "");
                    }


                } else {
                    TextView textViewTitle = view.findViewById(R.id.textViewTitle);
                    TextView textViewCount = view.findViewById(R.id.textViewCount);
                    textViewTitle.setText(textViewTitle.getText());
                    if (tab.getPosition() == 0) {
                        textViewCount.setText(WeChatScanDataRepository.getInstance().getVideoList().size() + "");
                    } else if (tab.getPosition() == 1) {
                        textViewCount.setText(WeChatScanDataRepository.getInstance().getImageList().size() + "");
                    } else if (tab.getPosition() == 2) {
                        textViewCount.setText(WeChatScanDataRepository.getInstance().getVoiceList().size() + "");
                    } else if (tab.getPosition() == 3) {
                        textViewCount.setText(WeChatScanDataRepository.getInstance().getFileList().size() + "");
                    } else if (tab.getPosition() == 4) {
                        textViewCount.setText(WeChatScanDataRepository.getInstance().getEmojiList().size() + "");
                    }
                    textViewTitle.setTextColor(Color.parseColor("#000000"));
                    textViewCount.setTextColor(Color.parseColor("#000000"));
                }
                tab.setCustomView(view);

            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                //tab.setCustomView(null);
                View view = tab.getCustomView();
                if (view == null) {
                    view = LayoutInflater.from(getActivity()).inflate(R.layout.tab_layout_header, null);
                    TextView textViewTitle = view.findViewById(R.id.textViewTitle);
                    TextView textViewCount = view.findViewById(R.id.textViewCount);
                    textViewTitle.setText(textViewTitle.getText());
                    textViewTitle.setTextColor(Color.parseColor("#ffffff"));
                    textViewCount.setTextColor(Color.parseColor("#ffffff"));
                    if (tab.getPosition() == 0) {
                        textViewCount.setText(WeChatScanDataRepository.getInstance().getVideoList().size() + "");
                    } else if (tab.getPosition() == 1) {
                        textViewCount.setText(WeChatScanDataRepository.getInstance().getImageList().size() + "");
                    } else if (tab.getPosition() == 2) {
                        textViewCount.setText(WeChatScanDataRepository.getInstance().getVoiceList().size() + "");
                    } else if (tab.getPosition() == 3) {
                        textViewCount.setText(WeChatScanDataRepository.getInstance().getFileList().size() + "");
                    } else if (tab.getPosition() == 4) {
                        textViewCount.setText(WeChatScanDataRepository.getInstance().getEmojiList().size() + "");
                    }


                } else {
                    TextView textViewTitle = view.findViewById(R.id.textViewTitle);
                    TextView textViewCount = view.findViewById(R.id.textViewCount);
                    textViewTitle.setText(textViewTitle.getText());
                    if (tab.getPosition() == 0) {
                        textViewCount.setText(WeChatScanDataRepository.getInstance().getVideoList().size() + "");
                    } else if (tab.getPosition() == 1) {
                        textViewCount.setText(WeChatScanDataRepository.getInstance().getImageList().size() + "");
                    } else if (tab.getPosition() == 2) {
                        textViewCount.setText(WeChatScanDataRepository.getInstance().getVoiceList().size() + "");
                    } else if (tab.getPosition() == 3) {
                        textViewCount.setText(WeChatScanDataRepository.getInstance().getFileList().size() + "");
                    } else if (tab.getPosition() == 4) {
                        textViewCount.setText(WeChatScanDataRepository.getInstance().getEmojiList().size() + "");
                    }
                    textViewTitle.setTextColor(Color.parseColor("#ffffff"));
                    textViewCount.setTextColor(Color.parseColor("#ffffff"));
                }
                tab.setCustomView(view);


                // 停止播放语音
                if (tab.getPosition() == 2) {
                    MediaPlayerUtils.stop();
                }
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

        viewPager2.setOffscreenPageLimit(5);

        new TabLayoutMediator(tabLayout, viewPager2, true, new TabLayoutMediator.TabConfigurationStrategy() {
            @Override
            public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {

                View view = tab.getCustomView();
                if (view == null) {
                    view = LayoutInflater.from(getActivity()).inflate(R.layout.tab_layout_header, null);
                    TextView textViewTitle = view.findViewById(R.id.textViewTitle);
                    TextView textViewCount = view.findViewById(R.id.textViewCount);
                    textViewTitle.setTextColor(Color.parseColor("#FFFFFF"));
                    textViewCount.setTextColor(Color.parseColor("#FFFFFF"));
                    textViewTitle.setText(TITLES[position]);


                    if (position == 0) {
                        textViewCount.setText(WeChatScanDataRepository.getInstance().getVideoList().size() + "");
                    } else if (position == 1) {
                        textViewCount.setText(WeChatScanDataRepository.getInstance().getImageList().size() + "");
                    } else if (position == 2) {
                        textViewCount.setText(WeChatScanDataRepository.getInstance().getVoiceList().size() + "");
                    } else if (position == 3) {
                        textViewCount.setText(WeChatScanDataRepository.getInstance().getFileList().size() + "");
                    } else if (position == 4) {
                        textViewCount.setText(WeChatScanDataRepository.getInstance().getEmojiList().size() + "");
                    }


                } else {
                    TextView textViewTitle = view.findViewById(R.id.textViewTitle);
                    TextView textViewCount = view.findViewById(R.id.textViewCount);
                    textViewTitle.setTextColor(Color.parseColor("#FFFFFF"));
                    textViewCount.setTextColor(Color.parseColor("#FFFFFF"));
                    textViewTitle.setText(TITLES[position]);
                    if (position == 0) {
                        textViewCount.setText(WeChatScanDataRepository.getInstance().getVideoList().size() + "");
                    } else if (position == 1) {
                        textViewCount.setText(WeChatScanDataRepository.getInstance().getImageList().size() + "");
                    } else if (position == 2) {
                        textViewCount.setText(WeChatScanDataRepository.getInstance().getVoiceList().size() + "");
                    } else if (position == 3) {
                        textViewCount.setText(WeChatScanDataRepository.getInstance().getFileList().size() + "");
                    } else if (position == 4) {
                        textViewCount.setText(WeChatScanDataRepository.getInstance().getEmojiList().size() + "");
                    }
                }
                tab.setCustomView(view);
            }
        }).attach();

        tabLayout.postDelayed(new Runnable() {
            @Override
            public void run() {

                String type = getArguments().getString("type");
                Log.e(TAG, "onActivityCreated: " + type);

//                "视频",
//                "图片",
//                "语音",
//                "文件",
//                "表情"
                if ("video".equals(type)) {
                    tabLayout.getTabAt(0).select();
                }
                if ("image".equals(type)) {
                    tabLayout.getTabAt(1).select();
                }
                if ("vocie".equals(type)) {
                    tabLayout.getTabAt(2).select();
                }
                if ("file".equals(type)) {
                    tabLayout.getTabAt(3).select();
                }
                if ("emoji".equals(type)) {
                    tabLayout.getTabAt(4).select();

                }
            }
        }, 1000);
    }

    public void updateStatus() {

        for (int i = 0; i < tabLayout.getTabCount(); i++) {
            View customView = tabLayout.getTabAt(i).getCustomView();

            if (customView != null) {
                TextView textViewCount = customView.findViewById(R.id.textViewCount);

                if (textViewCount != null) {
                    if (i == 0) {
                        textViewCount.setText(WeChatScanDataRepository.getInstance().getVideoList().size() + "");
                    } else if (i == 1) {
                        textViewCount.setText(WeChatScanDataRepository.getInstance().getImageList().size() + "");
                    } else if (i == 2) {
                        textViewCount.setText(WeChatScanDataRepository.getInstance().getVoiceList().size() + "");
                    } else if (i == 3) {
                        textViewCount.setText(WeChatScanDataRepository.getInstance().getFileList().size() + "");
                    } else if (i == 4) {
                        textViewCount.setText(WeChatScanDataRepository.getInstance().getEmojiList().size() + "");
                    }
                }
            }
        }
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.imageViewBack) {
            NavHostFragment.findNavController(DetailsFragment.this).navigate(R.id.clearFragment);
        }
    }
}
